package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.IdentityInfoDataClass;
import com.yjf.ljr.dataclass.IdentityInfoDataClass.IdentityInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class IdentityInfoActivity extends IjActivity implements View.OnClickListener {

	@ID("identity_name")
	private TextView identityName;// 真实姓名
	@ID("identity_ID")
	private TextView identityID;// 身份证号
	@ID("identity_status")
	private TextView identityStatus;// 认证状态
	@ID("identity_status_container")
	private RelativeLayout identityStatusContainer;
	@ID("status_right_arrow")
	private ImageView rightArrow;
	@ID("identity_type")
	private TextView identityType;
	@ID("identity_level")
	private TextView identityLevel;

	private String realName;// 真实姓名
	private String certNo;// 身份证号
	private String certifyStatus;// 认证状态
	private String certifyType;// 证件类型
	private String token;
	private String certifyLevel;// 实名等级
	private String realCertNo;// 身份证号，没遮掩

	private final int REALNAME_RESULT = 1005;
	private boolean isNeedFresh = false;

	private boolean isUpdate = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_identity_info);
		isNeedFresh = false;
		initControls();
		new IdentityInfoTask().execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initControls() {
		Button btnLeft = (Button) findViewById(R.id.btn_title_left);
		btnLeft.setOnClickListener(this);
		setTitleStr(getString(R.string.identity_info));
		identityStatusContainer.setOnClickListener(this);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_title_left:
			if (isNeedFresh) {
				setResult(RESULT_OK);
			} else {
				setResult(RESULT_CANCELED);
			}
			finish();
			break;
		case R.id.identity_status_container:

			if (!TextUtils.isEmpty(certifyStatus)
					&& ((certifyStatus.trim().equalsIgnoreCase("IS") && !isUpdate)
							|| certifyStatus.trim().equalsIgnoreCase("IN") || certifyStatus.trim().equalsIgnoreCase(
							"APPLY"))) {
				return;
			} else {
				Intent intent = new Intent(IdentityInfoActivity.this, IdentityVerifyActivity.class);
				intent.putExtra("token", token);
				intent.putExtra("isUpdate", isUpdate);
				intent.putExtra("name", realName);
				intent.putExtra("realCertNo", realCertNo);
				startActivityForResult(intent, REALNAME_RESULT);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (isNeedFresh) {
			setResult(RESULT_OK);
		} else {
			setResult(RESULT_CANCELED);
		}
		super.onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_OK && requestCode == REALNAME_RESULT) {
			isNeedFresh = true;
			new IdentityInfoTask().execute();
		}
	}

	class IdentityInfoTask extends AsyncTask<Void, Void, String> {
		private String mFailStr;
		private IdentityInfoDataClass dc = new IdentityInfoDataClass();

		@Override
		protected void onPreExecute() {
			showProgressDialog();
			isUpdate = false;
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject mObject = new RequestObject();
			mObject.method = "getRealNameInfo";
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				IdentityInfo data = dc.identityInfo;
				if (data != null) {
					realName = data.realName;// 真实姓名
					certNo = data.certNo;// 身份证号
					certifyStatus = data.realNameAuthentication;// 验证状态
					certifyType = data.certType;// 证件类型
					token = dc.token;
					certifyLevel = data.certifyLevel;
					realCertNo = data.realCertNo;
					if (!TextUtils.isEmpty(realName)) {
						identityName.setText(realName);
					}
					if (!TextUtils.isEmpty(certifyType)) {
						identityType.setText(certifyType);
					}
					if (!TextUtils.isEmpty(certNo)) {
						identityID.setText(certNo);
					}

					String statusStr = getString(R.string.identity_status_unrecognized);
					if (!TextUtils.isEmpty(certifyStatus)) {
						if (data.realNameAuthentication.equals("IS")) {// 通过
							statusStr = getString(R.string.identity_status_authenticated);
						} else if (data.realNameAuthentication.equals("IN")
								|| data.realNameAuthentication.equals("APPLY")) {// 申请中
							statusStr = getString(R.string.identity_status_verifying);
						} else if (data.realNameAuthentication.equals("NO")) {// 未通过
							statusStr = getString(R.string.identity_status_failed);
						} else {// 未认证
							statusStr = getString(R.string.identity_status_unrecognized);
						}
					}

					if (!TextUtils.isEmpty(certifyLevel)) {
						if (certifyLevel.equals("1")) {
							identityLevel.setText(getString(R.string.identity_level_quick_verify));
						} else if (certifyLevel.equals("0")) {
							identityLevel.setText(getString(R.string.identity_info_none));
						} else {
							identityLevel.setText(getString(R.string.identity_level_normal_verify));
						}
					}

					if (!TextUtils.isEmpty(statusStr)) {
						String authenticated = getString(R.string.identity_status_authenticated);
						String verify = getString(R.string.identity_status_verifying);
						String enhancedReal = getString(R.string.identity_status_enhanced_real_name_audit);
						String quickLevel = getString(R.string.identity_level_quick_verify);
						String verifying = getString(R.string.identity_status_verifying);
						if (quickLevel.equals(identityLevel.getText().toString())
								&& statusStr.trim().equals(authenticated)) {
							isUpdate = true;
							if (!TextUtils.isEmpty(data.realNameCertifyStatusEnum)
									&& (data.realNameCertifyStatusEnum.equals("CHECK_PASSING"))) {
								statusStr = getString(R.string.identity_status_enhanced_real_name_audit);
							} else {
								statusStr = getString(R.string.identity_level_update);
							}
						}

						if (quickLevel.equals(identityLevel.getText().toString())
								&& !statusStr.trim().equals(verifying)) {
							isUpdate = true;
						}

						if (statusStr.trim().equals(authenticated) || statusStr.trim().equals(verify)
								|| statusStr.trim().equals(enhancedReal)) {
							rightArrow.setVisibility(View.INVISIBLE);
							identityStatusContainer.setClickable(false);
						} else {
							identityStatusContainer.setClickable(true);
						}
						identityStatus.setText(statusStr);
					}
				}

			}
		}
	}

}
